# Copyright (C) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//tools/build:rules.bzl", "android_dynamic_library", "apic_compile", "apic_template", "cc_copts", "cc_dynamic_library")

apic_template(
    name = "gles_templated",
    api = "//gapis/api/gles:api",
    templates = [
        "//gapis/api/templates:api_imports.h",
        "//gapis/api/templates:api_spy.h",
        "//gapis/api/templates:api_spy.cpp",
        "//gapis/api/templates:api_types.h",
        "//gapis/api/templates:api_types.cpp",
        "//gapis/api/gles/templates:api_exports.cpp",
        "//gapis/api/gles/templates:api_imports.cpp",
    ],
)

apic_template(
    name = "vulkan_templated",
    api = "//gapis/api/vulkan:api",
    templates = [
        "//gapis/api/templates:api_imports.h",
        "//gapis/api/templates:api_spy.h",
        "//gapis/api/templates:api_spy.cpp",
        "//gapis/api/templates:api_types.h",
        "//gapis/api/templates:api_types.cpp",
        "//gapis/api/vulkan/templates:api_exports.cpp",
        "//gapis/api/vulkan/templates:api_imports.cpp",
        "//gapis/api/vulkan/templates:vk_spy_helpers.cpp",
    ],
)

apic_template(
    name = "gvr_templated",
    api = "//gapis/api/gvr:api",
    templates = [
        "//gapis/api/templates:api_imports.h",
        "//gapis/api/templates:api_spy.h",
        "//gapis/api/templates:api_spy.cpp",
        "//gapis/api/templates:api_types.h",
        "//gapis/api/templates:api_types.cpp",
        "//gapis/api/gvr/templates:api_exports.cpp",
        "//gapis/api/gvr/templates:api_exports.h",
        "//gapis/api/gvr/templates:api_imports.cpp",
    ],
)

apic_compile(
    name = "apis_compiled",
    apis = [
        "//gapis/api/gles:api",
        "//gapis/api/vulkan:api",
        "//gapis/api/gvr:api",
    ],
    emit = ["encode"],
    namespace = "gapii",
    optimize = True,
)

apic_template(
    name = "gvr_install",
    api = "//gapis/api/gvr:api",
    templates = [
        "//gapis/api/gvr/templates:api_install.cpp",
    ],
)

apic_template(
    name = "osx_opengl_framework",
    api = "//gapis/api/gles:api",
    templates = [
        "//gapis/api/templates:opengl_framework_exports.cpp",
    ],
)

apic_template(
    name = "windows_opengl32",
    api = "//gapis/api/gles:api",
    templates = [
        "//gapis/api/templates:opengl32_resolve.cpp",
        #TODO "//gapis/api/templates:opengl32_exports.def"
        #TODO "//gapis/api/templates:opengl32_x64.asm",
    ],
)

cc_library(
    name = "cc",
    srcs = glob(
        [
            "*.cpp",
            "*.h",
            "*.inc",
        ],
        exclude = [
            "*_test.cpp",
        ],
    ) + select({
        "//tools/build:linux": [],
        "//tools/build:darwin": [":osx_opengl_framework"],
        "//tools/build:windows": [":windows_opengl32"] + glob([
            "windows/*.cpp",
            "windows/*.h",
        ]),
        # Android
        "//conditions:default": glob([
            "android/*.cpp",
            "android/*.h",
        ]) + [":gvr_install"],
    }) + [
        ":apis_compiled",
        ":gles_templated",
        ":gvr_templated",
        ":vulkan_templated",
    ],
    copts = cc_copts() + select({
        "//tools/build:windows": ["-Wa,-mbig-obj"],
        "//conditions:default": [],
    }),
    linkopts = select({
        "//tools/build:linux": [],
        "//tools/build:darwin": [
            "-framework Cocoa",
            "-framework OpenGL",
        ],
        "//tools/build:windows": [],
        # Android
        "//conditions:default": [
            "-ldl",
            "-llog",
            "-lm",
            "-Wl,--no-undefined",
        ],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "//core/cc",
        "//core/memory/arena/cc",
        "//core/memory_tracker/cc",
        "//core/os/device/deviceinfo/cc",
        "//gapil/runtime/cc",
        "//gapis/api:api_cc_proto",
        "//gapis/api/gles/gles_pb:extras_cc_proto",
        "//gapis/api/vulkan/vulkan_pb:extras_cc_proto",
        "//gapis/capture:capture_cc_proto",
        "//gapis/memory/memory_pb:memory_pb_cc_proto",
        "@com_google_protobuf//:protobuf",
        "@spirv_reflect//:spirv-reflect",
    ],
    alwayslink = True,
)

cc_dynamic_library(
    name = "libgapii",
    visibility = ["//visibility:public"],
    exports = "gapii_desktop.exports",
    deps = [":cc"],
)

android_dynamic_library(
    name = "libgapii_android",
    visibility = ["//visibility:public"],
    exports = "gapii_android.exports",
    deps = [":cc"],
)
